home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / pc / Shout3Ddemo / Shout3d_runtime / codebase / custom_nodes / FogEffect.java < prev    next >
Text File  |  2000-10-03  |  3KB  |  93 lines

  1. /**    
  2.     Company:        Eyematic Interfaces
  3.     Project:        Shout3D 2.0 Sample Code
  4.     Class:            FogEffect
  5.     Date:            March 23, 2000
  6.     Description:    A simple example of a PostRenderEffect that adds a fog to the scene.
  7.     (C) Copyright Eyematic Interfaces, Inc. - 1997-2000 - All rights reserved
  8.  */
  9.  
  10. package custom_nodes;
  11. import shout3d.core.*;
  12. import shout3d.*;
  13. import java.awt.Graphics;
  14.  
  15. /**
  16.  * FogEffect
  17.  * 
  18.  * @author Paul Isaacs
  19.  */
  20.  
  21. public class FogEffect extends PostRenderEffect {
  22.  
  23.     // fogColor is the color that the scene fades to.
  24.     // Objects closer than fogNear will render fully as their own color
  25.     // Objects between fogNear and fogFar will fade linearly from their own color to fogColor
  26.     // Objects farther than fogFar will be rendered fully as fogFar
  27.     final public FloatArrayField  fogColor      = new FloatArrayField(this, "fogColor", Field.COLOR, new float[]{0,0,0});
  28.     final public FloatField       fogNear     = new FloatField(        this, "fogNear", Field.ANY, 0);
  29.     final public FloatField       fogFar      = new FloatField(        this, "fogFar", Field.ANY, 50);
  30.     /**
  31.      * Constructs a default FogEffect node.
  32.      */
  33.     public FogEffect(){}
  34.     
  35.     public void filter(Graphics offScreenGraphics, int surface_pixel_bits[], float z_buffer[], int deviceWidth, int deviceHeight){
  36.         if (surface_pixel_bits == null || z_buffer == null)
  37.             return;
  38.         
  39.         // Get the fog color components into byte form.
  40.         int fogR = (int)(fogColor.getValue()[0]*255) &0xff;
  41.         int fogG = (int)(fogColor.getValue()[1]*255) &0xff;
  42.         int fogB = (int)(fogColor.getValue()[2]*255) &0xff;
  43.         // Put the fog color components into a single aggregate int, suitable
  44.         // for plopping into the array of pixels.
  45.         int fogColorInt = 0xff000000 + (fogR<<16) + (fogG<<8) + fogB;
  46.         
  47.         // Handy variables.
  48.         float f_far  = fogFar.getValue();
  49.         float f_near = fogNear.getValue();
  50.         float farMinusNear = f_far - f_near;
  51.         
  52.         float zDist, normZDist;
  53.         
  54.         float objRatio;
  55.         int   imgR, imgG, imgB;
  56.         
  57.         // Iterate over all the pixels.
  58.         for (int i=0;i<surface_pixel_bits.length;i++){
  59.             if (z_buffer[i] <= 0) {
  60.                 // Background pixels get the fog color.
  61.                 surface_pixel_bits[i] = fogColorInt;
  62.             }
  63.             else {
  64.                 // z_buffer contents are actually entered as 1/z.
  65.                 // Need to use reciprocal to get distance.
  66.                 zDist = 1 / z_buffer[i];
  67.                 
  68.                 // Get normZDist.  This is 0 if closer than fogNear, 1 if farther 
  69.                 // than fogFar, and interpolates linearly between them.
  70.                 if (zDist >= f_far)
  71.                     normZDist = 1f;
  72.                 else if (zDist <= f_near)
  73.                     normZDist = 0f;
  74.                 else
  75.                     normZDist = (float)((zDist - f_near)/farMinusNear); 
  76.                                                            
  77.                 // The pixel gets a contribution of normZDist times the fog color,
  78.                 // plus (1-normZDist) times the color in the image.
  79.                 objRatio = (float)(1.0 - normZDist);
  80.                 imgR = (surface_pixel_bits[i]>>16)&0xff;
  81.                 imgG = (surface_pixel_bits[i]>>8)&0xff;
  82.                 imgB =  surface_pixel_bits[i] &0xff;
  83.                 surface_pixel_bits[i] = 0xff000000 +
  84.                           (((int)(objRatio * imgR + normZDist * fogR)&0xff)<<16) +
  85.                           (((int)(objRatio * imgG + normZDist * fogG)&0xff)<<8) +
  86.                            ((int)(objRatio * imgB + normZDist * fogB)&0xff);
  87.             }
  88.         }
  89.     }
  90. }
  91.  
  92.  
  93.